/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.entity;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thinkgem.jeesite.common.persistence.DataEntity;

/**
 * 商品管理Entity
 * @author xxs
 * @version 2016-03-21
 */
public class Goods extends DataEntity<Goods> {
	
	//VG--ViewGoods
	public static final String DEFAULT_GOODSVIEW = "goodsDetail";
	
	public static final int ATTRIBUTE_VALUE_PROPERTY_COUNT = 20;

	public static final String ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX = "attributeValue";

	
	private static final long serialVersionUID = 1L;
	private String type;		// 类型
	private GoodsCategory goodsCategory;		// 分类
	private String sn;		// 货号
	private String name;		// 商品名称
	private String caption;		// 副标题
	private String price;		// 价格
	private String marketPrice;		// 市场价
	private Long sales;		// 销售价
	private String unit;		// 单位
	private String weight;		// 重量
	private String introduction;		// 商品描述
	private String isDelivery;		// 是否需要物流
	private String isMarketable;		// 是否上架
	private String isTop;		// 是否置顶
	private String keyword;		// 关键字
	private String memo;		// 备注
	private String image;		// 封面图
	private String productImages;		// 图片集
	private String version;		// 版本
	private String seoDescription;		// seo description
	private String seoKeywords;		// seo keywords
	private String seoTitle;		// seo title
	private String shopTag;		// 标签
	private Brand brand;		// 品牌
	private String score;		// 积分
	private String attributeValue0;		// attribute_value0
	private String attributeValue1;		// attribute_value1
	private String attributeValue10;		// attribute_value10
	private String attributeValue11;		// attribute_value11
	private String attributeValue12;		// attribute_value12
	private String attributeValue13;		// attribute_value13
	private String attributeValue14;		// attribute_value14
	private String attributeValue15;		// attribute_value15
	private String attributeValue16;		// attribute_value16
	private String attributeValue17;		// attribute_value17
	private String attributeValue18;		// attribute_value18
	private String attributeValue19;		// attribute_value19
	private String attributeValue2;		// attribute_value2
	private String attributeValue3;		// attribute_value3
	private String attributeValue4;		// attribute_value4
	private String attributeValue5;		// attribute_value5
	private String attributeValue6;		// attribute_value6
	private String attributeValue7;		// attribute_value7
	private String attributeValue8;		// attribute_value8
	private String attributeValue9;		// attribute_value9
    private String customContentView;	// 自定义内容视图
   	private String viewConfig;	// 视图参数
	
   	private String sis;	
   	private List<SpecificationItem> specificationItems = new ArrayList<SpecificationItem>();
   	private String pvs;	
   	private List<ParameterValue> parameterValues = new ArrayList<ParameterValue>();
   	
   	private Set<Member> favoriteMembers = new HashSet<Member>();

	private Set<Product> products = new HashSet<Product>();
   	
	public Goods() {
		super();
	}

	public Goods(String id){
		super(id);
	}
	
	public Goods(GoodsCategory goodsCategory){
		this();
		this.goodsCategory = goodsCategory;
	}
	
	public Set<Member> getFavoriteMembers() {
		return favoriteMembers;
	}

	public void setFavoriteMembers(Set<Member> favoriteMembers) {
		this.favoriteMembers = favoriteMembers;
	}

	public Set<Product> getProducts() {
		return products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}
	
	@Valid
	@Column(length = 4000)
	public List<SpecificationItem> getSpecificationItems() {
		return specificationItems;
	}

	public void setSpecificationItems(List<SpecificationItem> specificationItems) {
		this.specificationItems = specificationItems;
	}
	
	@Valid
	@Column(length = 4000)
	public List<ParameterValue> getParameterValues() {
		return parameterValues;
	}

	public void setParameterValues(List<ParameterValue> parameterValues) {
		this.parameterValues = parameterValues;
	}

	@Length(min=1, max=255, message="类型长度必须介于 1 和 255 之间")
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	public String getShopTag() {
		return shopTag;
	}
	
	public void setShopTag(String shopTag) {
		this.shopTag = shopTag;
	}
	@Length(min=1, max=255, message="Pvs长度必须介于 1 和 255 之间")
	public String getPvs() {
		return pvs;
	}

	public void setPvs(String pvs) {
		this.pvs = pvs;
	}
	
	@JsonIgnore
	@NotNull(message="商品分类不能为空")
	public GoodsCategory getGoodsCategory() {
		return goodsCategory;
	}

	public void setGoodsCategory(GoodsCategory goodsCategory) {
		this.goodsCategory = goodsCategory;
	}
	
	@Length(min=1, max=255, message="货号长度必须介于 1 和 255 之间")
	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}
	
	@Length(min=1, max=255, message="商品名称长度必须介于 1 和 255 之间")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Length(min=0, max=255, message="副标题长度必须介于 0 和 255 之间")
	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}
	
	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}
	
	public String getMarketPrice() {
		return marketPrice;
	}
	
	public void setMarketPrice(String marketPrice) {
		this.marketPrice = marketPrice;
	}
	
	public String getSis() {
		return sis;
	}

	public void setSis(String sis) {
		this.sis = sis;
	}
	
	@NotNull(message="销售价不能为空")
	public Long getSales() {
		return sales;
	}

	public void setSales(Long sales) {
		this.sales = sales;
	}
	
	@Length(min=0, max=255, message="单位长度必须介于 0 和 255 之间")
	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}
	
	@Length(min=0, max=11, message="重量长度必须介于 0 和 11 之间")
	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}
	
	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}
	
	public String getIsDelivery() {
		return isDelivery;
	}

	public void setIsDelivery(String isDelivery) {
		this.isDelivery = isDelivery;
	}
	
	public String getIsMarketable() {
		return isMarketable;
	}

	public void setIsMarketable(String isMarketable) {
		this.isMarketable = isMarketable;
	}
	
	public String getIsTop() {
		return isTop;
	}

	public void setIsTop(String isTop) {
		this.isTop = isTop;
	}
	
	@Length(min=0, max=255, message="关键字长度必须介于 0 和 255 之间")
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	@Length(min=0, max=255, message="备注长度必须介于 0 和 255 之间")
	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
	
	@Length(min=0, max=255, message="封面图长度必须介于 0 和 255 之间")
	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}
	
	public String getProductImages() {
		return productImages;
	}

	public void setProductImages(String productImages) {
		this.productImages = productImages;
	}
	
	@Length(min=0, max=255, message="版本控制长度必须介于 0 和 255 之间")
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}
	
	@Length(min=0, max=255, message="seo description长度必须介于 0 和 255 之间")
	public String getSeoDescription() {
		return seoDescription;
	}

	public void setSeoDescription(String seoDescription) {
		this.seoDescription = seoDescription;
	}
	
	@Length(min=0, max=255, message="seo keywords长度必须介于 0 和 255 之间")
	public String getSeoKeywords() {
		return seoKeywords;
	}

	public void setSeoKeywords(String seoKeywords) {
		this.seoKeywords = seoKeywords;
	}
	
	@Length(min=0, max=255, message="seo title长度必须介于 0 和 255 之间")
	public String getSeoTitle() {
		return seoTitle;
	}

	public void setSeoTitle(String seoTitle) {
		this.seoTitle = seoTitle;
	}
	
	@JsonIgnore
	@NotNull(message="商品品牌不能为空")
	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	
	public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}
	
	@Length(min=0, max=255, message="attribute_value0长度必须介于 0 和 255 之间")
	public String getAttributeValue0() {
		return attributeValue0;
	}

	public void setAttributeValue0(String attributeValue0) {
		this.attributeValue0 = attributeValue0;
	}
	
	@Length(min=0, max=255, message="attribute_value1长度必须介于 0 和 255 之间")
	public String getAttributeValue1() {
		return attributeValue1;
	}

	public void setAttributeValue1(String attributeValue1) {
		this.attributeValue1 = attributeValue1;
	}
	
	@Length(min=0, max=255, message="attribute_value10长度必须介于 0 和 255 之间")
	public String getAttributeValue10() {
		return attributeValue10;
	}

	public void setAttributeValue10(String attributeValue10) {
		this.attributeValue10 = attributeValue10;
	}
	
	@Length(min=0, max=255, message="attribute_value11长度必须介于 0 和 255 之间")
	public String getAttributeValue11() {
		return attributeValue11;
	}

	public void setAttributeValue11(String attributeValue11) {
		this.attributeValue11 = attributeValue11;
	}
	
	@Length(min=0, max=255, message="attribute_value12长度必须介于 0 和 255 之间")
	public String getAttributeValue12() {
		return attributeValue12;
	}

	public void setAttributeValue12(String attributeValue12) {
		this.attributeValue12 = attributeValue12;
	}
	
	@Length(min=0, max=255, message="attribute_value13长度必须介于 0 和 255 之间")
	public String getAttributeValue13() {
		return attributeValue13;
	}

	public void setAttributeValue13(String attributeValue13) {
		this.attributeValue13 = attributeValue13;
	}
	
	@Length(min=0, max=255, message="attribute_value14长度必须介于 0 和 255 之间")
	public String getAttributeValue14() {
		return attributeValue14;
	}

	public void setAttributeValue14(String attributeValue14) {
		this.attributeValue14 = attributeValue14;
	}
	
	@Length(min=0, max=255, message="attribute_value15长度必须介于 0 和 255 之间")
	public String getAttributeValue15() {
		return attributeValue15;
	}

	public void setAttributeValue15(String attributeValue15) {
		this.attributeValue15 = attributeValue15;
	}
	
	@Length(min=0, max=255, message="attribute_value16长度必须介于 0 和 255 之间")
	public String getAttributeValue16() {
		return attributeValue16;
	}

	public void setAttributeValue16(String attributeValue16) {
		this.attributeValue16 = attributeValue16;
	}
	
	@Length(min=0, max=255, message="attribute_value17长度必须介于 0 和 255 之间")
	public String getAttributeValue17() {
		return attributeValue17;
	}

	public void setAttributeValue17(String attributeValue17) {
		this.attributeValue17 = attributeValue17;
	}
	
	@Length(min=0, max=255, message="attribute_value18长度必须介于 0 和 255 之间")
	public String getAttributeValue18() {
		return attributeValue18;
	}

	public void setAttributeValue18(String attributeValue18) {
		this.attributeValue18 = attributeValue18;
	}
	
	@Length(min=0, max=255, message="attribute_value19长度必须介于 0 和 255 之间")
	public String getAttributeValue19() {
		return attributeValue19;
	}

	public void setAttributeValue19(String attributeValue19) {
		this.attributeValue19 = attributeValue19;
	}
	
	@Length(min=0, max=255, message="attribute_value2长度必须介于 0 和 255 之间")
	public String getAttributeValue2() {
		return attributeValue2;
	}

	public void setAttributeValue2(String attributeValue2) {
		this.attributeValue2 = attributeValue2;
	}
	
	@Length(min=0, max=255, message="attribute_value3长度必须介于 0 和 255 之间")
	public String getAttributeValue3() {
		return attributeValue3;
	}

	public void setAttributeValue3(String attributeValue3) {
		this.attributeValue3 = attributeValue3;
	}
	
	@Length(min=0, max=255, message="attribute_value4长度必须介于 0 和 255 之间")
	public String getAttributeValue4() {
		return attributeValue4;
	}

	public void setAttributeValue4(String attributeValue4) {
		this.attributeValue4 = attributeValue4;
	}
	
	@Length(min=0, max=255, message="attribute_value5长度必须介于 0 和 255 之间")
	public String getAttributeValue5() {
		return attributeValue5;
	}

	public void setAttributeValue5(String attributeValue5) {
		this.attributeValue5 = attributeValue5;
	}
	
	@Length(min=0, max=255, message="attribute_value6长度必须介于 0 和 255 之间")
	public String getAttributeValue6() {
		return attributeValue6;
	}

	public void setAttributeValue6(String attributeValue6) {
		this.attributeValue6 = attributeValue6;
	}
	
	@Length(min=0, max=255, message="attribute_value7长度必须介于 0 和 255 之间")
	public String getAttributeValue7() {
		return attributeValue7;
	}

	public void setAttributeValue7(String attributeValue7) {
		this.attributeValue7 = attributeValue7;
	}
	
	@Length(min=0, max=255, message="attribute_value8长度必须介于 0 和 255 之间")
	public String getAttributeValue8() {
		return attributeValue8;
	}

	public void setAttributeValue8(String attributeValue8) {
		this.attributeValue8 = attributeValue8;
	}
	
	@Length(min=0, max=255, message="attribute_value9长度必须介于 0 和 255 之间")
	public String getAttributeValue9() {
		return attributeValue9;
	}

	public void setAttributeValue9(String attributeValue9) {
		this.attributeValue9 = attributeValue9;
	}
    public String getCustomContentView() {
        return customContentView;
    }

    public void setCustomContentView(String customContentView) {
        this.customContentView = customContentView;
    }

    public String getViewConfig() {
        return viewConfig;
    }

    public void setViewConfig(String viewConfig) {
        this.viewConfig = viewConfig;
    }
    
	public void removeAttributeValue() {
		for (int i = 0; i < ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) {
			String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i;
			try {
				PropertyUtils.setProperty(this, propertyName, null);
			} catch (IllegalAccessException e) {
				throw new RuntimeException(e.getMessage(), e);
			} catch (InvocationTargetException e) {
				throw new RuntimeException(e.getMessage(), e);
			} catch (NoSuchMethodException e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}
	
	public String getAttributeValue(Attribute attribute) {
		if (attribute == null || attribute.getPropertyIndex() == null) {
			return null;
		}

		try {
			String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + attribute.getPropertyIndex();
			return (String) PropertyUtils.getProperty(this, propertyName);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
	public void setAttributeValue(Attribute attribute, String attributeValue) {
		if (attribute == null || attribute.getPropertyIndex() == null) {
			return;
		}
		try {
			String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + attribute.getPropertyIndex();
			PropertyUtils.setProperty(this, propertyName, attributeValue);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
	@Transient
	public boolean hasSpecification() {
		return CollectionUtils.isNotEmpty(getSpecificationItems());
	}
}